using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.IO;
using System.Xml;

namespace EventLogger
{
    public class cEventLogger
    {
        /// <summary>
        /// An enumeration to select Event Type
        /// </summary>
        public enum EventsType
        {
            Error, Warning, Information, SqlTrace, Success
        }

        /// <summary>
        /// This Function is used for Information,Warning,Success Event type log file 
        /// </summary>
        /// <param name="eEventType">As enumeration EventsType (Error, Warning, Information, SqlTrace, Success)</param>
        /// <param name="Title">As string</param>
        /// <param name="sDescription">As string</param>
        /// <param name="Source">As string</param>
        public static void AddEntry(EventsType eEventType, string Title, string sDescription, string Source)
        {
            string sFullFilePath = Func_GetFilePath();
            Func_CreateXML(eEventType, new Exception(), sDescription, sFullFilePath, Source, Title);
        }

        /// <summary>
        /// This Function is used for Errors,SqlTrace Event type log file 
        /// </summary>
        /// <param name="eEventType">As enumeration EventsType (Error, Warning, Information, SqlTrace, Success)</param>
        /// <param name="Title">As string</param>
        /// <param name="sDescription">As string</param>
        /// <param name="Source">As string</param>
        /// <param name="exException">As Exception</param>
        public static void AddEntry(EventsType eEventType, string Title, string sDescription, string Source, Exception exException)
        {
            string sFullFilePath = Func_GetFilePath();
            Func_CreateXML(eEventType, exException, sDescription, sFullFilePath, Source, Title);
        }
        
        private static string Func_GetFilePath()
        {
            string FullPath = string.Empty;
            string RootPath = Func_GetRootPath();

            string LogFolderName = "LogFiles";
            FullPath = RootPath + "\\" + LogFolderName;
            Func_CheckDirectory(FullPath);

            string YearName = DateTime.Now.Year.ToString();
            FullPath += "\\" + YearName;
            Func_CheckDirectory(FullPath);

            string MonthName = DateTime.Now.Month.ToString();
            if (int.Parse(MonthName) < 10)
            {
                MonthName = "0" + MonthName;
            }
            FullPath += "\\" + MonthName;
            Func_CheckDirectory(FullPath);

            string FileName = DateTime.Now.ToString("''dd'.xml'");
            FullPath += "\\" + FileName;

            return FullPath;
        }

        private static string Func_GetRootPath()
        {
            string DirectoryPath = string.Empty;
            System.Reflection.Assembly a = System.Reflection.Assembly.GetEntryAssembly();
            DirectoryPath = System.IO.Path.GetDirectoryName(a.Location);
            return DirectoryPath;
        }

        private static void Func_CheckDirectory(string DirPath)
        {
            if (!Directory.Exists(DirPath))
            {
                Directory.CreateDirectory(DirPath);
            }
        }

        private static void Func_CreateXML(EventsType eEventType, Exception exException, string sDescription, string sFullPathName, string Source, string Title)
        {
            #region CheckAndCreateXML... 
            XmlDocument xmlDoc = new XmlDocument();
            if (File.Exists(sFullPathName))
            {
                xmlDoc.Load(sFullPathName);
            }
            else
            {
                XmlTextWriter xmlWriter = new XmlTextWriter(sFullPathName, System.Text.Encoding.UTF8);
                xmlWriter.Formatting = Formatting.Indented;
                xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
                xmlWriter.WriteStartElement("LogEvents");
                string[] sProjectName = System.AppDomain.CurrentDomain.BaseDirectory.Split('\\');
                xmlWriter.WriteComment("This File was created at " + DateTime.Now.ToString("hh:mm:ss tt dddd, MMMM, dd yyyy") + ", for LogFile of " + sProjectName[sProjectName.Length - 2].ToString() + " application.");
                xmlWriter.Close();
                xmlDoc.Load(sFullPathName);
            }
            #endregion

            AddDataEntry(xmlDoc, eEventType, Title, sDescription, Source, exException);
            xmlDoc.Save(sFullPathName);
        }

        private static void AddDataEntry(XmlDocument xmlDoc, EventsType eEventType, string Title, string sDescription, string Source, Exception exException)
        {
            XmlElement Entry = xmlDoc.CreateElement("DataEntry");
            xmlDoc.ChildNodes[1].AppendChild(Entry);
            
            #region Type Node...
            XmlElement childNode1 = xmlDoc.CreateElement("Type");
            Entry.AppendChild(childNode1);
            XmlText textNode1 = xmlDoc.CreateTextNode(eEventType.ToString());
            childNode1.AppendChild(textNode1);
            #endregion
            #region Date Node...
            XmlElement childNode2 = xmlDoc.CreateElement("Date");
            Entry.AppendChild(childNode2);
            XmlText textNode2 = xmlDoc.CreateTextNode(DateTime.Now.ToString("ddd dd/MM/yyyy"));
            childNode2.AppendChild(textNode2);
            #endregion
            #region Time Node...
            XmlElement childNode3 = xmlDoc.CreateElement("Time");
            Entry.AppendChild(childNode3);
            XmlText textNode3 = xmlDoc.CreateTextNode(DateTime.Now.ToString("hh:mm:ss tt"));
            childNode3.AppendChild(textNode3);
            #endregion
            #region Title Node...
            XmlElement childNode4 = xmlDoc.CreateElement("Title");
            Entry.AppendChild(childNode4);
            XmlText textNode4 = xmlDoc.CreateTextNode(Title);
            childNode4.AppendChild(textNode4);
            #endregion
            #region Source Node...
            XmlElement childNode5 = xmlDoc.CreateElement("Source");
            Entry.AppendChild(childNode5);
            XmlText textNode5 = xmlDoc.CreateTextNode(Source);
            childNode5.AppendChild(textNode5);
            #endregion
            #region LogDetail Node...
            XmlElement childNode6 = xmlDoc.CreateElement("LogDetail");
            Entry.AppendChild(childNode6);
            XmlText textNode6;

            if (exException.StackTrace != null)
            {
                string[] sNewErrorName = exException.ToString().Split(':');
                string sErrorName = sNewErrorName[0].ToString();
                textNode6 = xmlDoc.CreateTextNode("Description:" + sDescription 
                            + "\nExceptionName:" + sErrorName
                            + "\nExceptionMessage:" + exException.Message.ToString()
                            + "\nStackTrace:" + exException.StackTrace.ToString());
            }
            else
            {
                textNode6 = xmlDoc.CreateTextNode(sDescription);
            }
            childNode6.AppendChild(textNode6);
            #endregion
        }

    }
}
